package _2FindAlgorithm.src.BinarySearch;

public class Demo3_BinarySearch_Java_leftmost {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 2, 4, 4, 5, 6, 7};
        int i = binarySearchBasic(arr, 3);
        System.out.println(i);
    }

    public static int binarySearchBasic(int[] a, int target) {
        int min = 0;
        int max = a.length - 1;
        int caniddate = -1;

        while(min <= max) {
            int middle = min + max >>> 1;
            if (target < a[middle]) {
                max = middle - 1;
            } else if (a[middle] < target) {
                min = middle + 1;
            } else {
                caniddate = middle;//修改给定目标索引位置
                max = middle - 1;//继续向左查找
            }
        }
        return caniddate;
    }
}
